/***************************************************************************
 * This document contains confidential and proprietary information
 * subject to non-disclosure agreements with AsiaInspection. This
 * information shall not be distributed or copied without written
 * permission from the AsiaInspection.
 ***************************************************************************/
package gui;

/***************************************************************************
 * <PRE>
 * Project Name    : report-service
 * <p>
 * Package Name    : com.ai.reportservice.controller
 * <p>
 * File Name       : TestImage.java
 * <p>
 * Creation Date   : Mar 20, 2016
 * <p>
 * Author          : Allen Zhang
 * <p>
 * Purpose         : TODO
 * <p>
 * <p>
 * History         : TODO
 * <p>
 * </PRE>
 ***************************************************************************/

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;

public class TestImage {
//	String path = "D:\\Allen\\devs\\pdf2image\\";
	String path = "C:\\middleware\\CACHE\\";

	public void convertPdf() throws IOException {
//		String pdfFilename = path + "Fulgent Sun Footwear_2015-August-21_1.pdf";
		String pdfFilename = path + "R-us3-1610645-4-75-279.534_1_1_3.pdf";
//		PDDocument document = PDDocument.loadNonSeq(new File(pdfFilename), null);
//		List<PDPage> pdPages = document.getDocumentCatalog().getAllPages();
//		int page = 0;
//		for (PDPage pdPage : pdPages) {
//			++page;
//			BufferedImage bim = pdPage.convertToImage(BufferedImage.TYPE_INT_RGB, 150);
//			String imgPath = pdfFilename + "-" + page + ".jpg";
//			ImageIOUtil.writeImage(bim, imgPath, 150);
//			//done, convert it to base64 array
//			System.out.println(encodeImageBase64(imgPath));
////			System.out.println(encodeImageBase64(new File(imgPath)));
//
//		}
//		document.close();
		System.out.println("xxx");

	}

//	private String encodeImageBase64(File image) {
	private String encodeImageBase64(String imgPath) {
		try {
//			Base64 base = new Base64();
			FileInputStream imgStream = new FileInputStream(imgPath);
			return new String(Base64.encodeBase64(IOUtils.toByteArray(imgStream)));
//			byte[] imgBase64Arr = base.encode(FileUtils.readFileToByteArray(imgStream));
//			return Arrays.toString(imgBase64Arr);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void concatJPG() throws IOException {
		String file = "Fulgent Sun Footwear_2015-August-21_1.pdf-";
		File file1 = new File(path + file + "1.jpg");
		File file2 = new File(path + file + "2.jpg");

		BufferedImage img1 = ImageIO.read(file1);
		BufferedImage img2 = ImageIO.read(file2);

		int widthImg1 = img1.getWidth();
		int heightImg1 = img1.getHeight();

		int widthImg2 = img2.getWidth();
		int heightImg2 = img2.getHeight();

		BufferedImage img = new BufferedImage(
				widthImg1 + widthImg2, // Final image will have width and height as
				heightImg1 + heightImg2, // addition of widths and heights of the images we already have
				BufferedImage.TYPE_INT_RGB);

		boolean image1Drawn = img.createGraphics().drawImage(img1, 0, 0, null); // 0, 0 are the x and y positions
		if (!image1Drawn)
			System.out.println("Problems drawing first image"); //where we are placing image1 in final image

		boolean image2Drawn = img.createGraphics().drawImage(img2, widthImg1, 0, null); // here width is mentioned as width of
		if (!image2Drawn)
			System.out.println("Problems drawing second image"); // image1 so both images will come in same level
// horizontally
		File final_image = new File(path +  "Final.jpg"); // “png can also be used here”
		boolean final_Image_drawing = ImageIO.write(img, "jpeg", final_image); //if png is used, write “png” instead “jpeg”
		if (!final_Image_drawing) System.out.println("Problems drawing final image");
	}

	public static void main(String args[]) throws IOException {
		TestImage ti = new TestImage();
		ti.convertPdf();
//		ti.concatJPG();
	}
}
